package com.bvgol.examples.my.lambda;/**
 * @Classname ListOP
 * @Description TODO
 * @Date 2021/4/22 17:32
 * @Created by GUOCHEN
 */

import com.bvgol.examples.my.list.MyList;
import com.bvgol.examples.my.stream.User;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;

import static java.util.Comparator.comparingInt;
import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toCollection;

/**
 * @program: my-spring-completely
 * @description: 集合的差集/交集/并集
 * @author: GUOCHEN
 * @create: 2021/04/22 17:32
 */
public class ListOP {
    static List<User> users_a = MyList.initUserList();
    static List<User> users_b = MyList.initUserListSameEques();

    /**
     * 功能描述: 根据对象的某个属性排序 这么简单?
     *
     * @param
     * @return: void
     * @author: 郭辰
     * @date: 2021/4/22 17:47
     */
    public static void sortByName() {
        users_a.forEach(System.out::println);
        System.out.println("-----------------");
        users_a.stream().sorted(Comparator.comparing(User::getName)).forEach(System.out::println);
        System.out.println("-----------------");
        //多参数排序
        users_a.stream().sorted(Comparator.comparing(User::getName).thenComparing(User::getAge)).forEach(System.out::println);
    }

    /**
     * 功能描述: 交集  A&B
     *
     * @param
     * @return: void
     * @author: 郭辰
     * @date: 2021/4/22 18:00
     */
    public static void AInnerJoinB() {
//        users_a.forEach(System.out::println);
//        System.out.println("-----------------");
//        users_b.forEach(System.out::println);
//        System.out.println("-----------------");
//
//        //根据参数取交集   A和B 都有的 名字(参数)
//        users_a.stream().filter(item -> users_b.stream().map(User::getName).collect(collectors.toList()).contains(item.getName()))
//                .collect(collectors.toList()).forEach(System.out::println);
//        System.out.println("-----------------");
//        //根据参数取差集   A中有 B中没有
//        users_a.stream().filter(item -> !users_b.stream().map(User::getName).collect(collectors.toList()).contains(item.getName()))
//                .collect(collectors.toList()).forEach(System.out::println);
        //并集
        List<User> list = new LinkedList<>();
        list.addAll(users_a);
        list.addAll(users_b);

        list.stream().distinct().forEach(System.out::println);
        System.out.println("-----------------");
        System.out.println("根据age去重");
        //根据id去重
        list.stream().collect(
                collectingAndThen(
                        toCollection(() -> new TreeSet<>(comparingInt(User::getAge))), ArrayList::new)
        ).forEach(System.out::println);
        System.out.println("-----------------");
        System.out.println("根据名字去重");
        //根据名字去重
        list.stream().filter(distinctByKey(User::getName)).forEach(System.out::println);

    }

    public static <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor) {
        Map<Object, Boolean> map = new ConcurrentHashMap<>();
        return t -> map.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
    }

// remove duplicate

    public static void main(String[] args) {
//        ListOP.sortByName();
        ListOP.AInnerJoinB();
    }
}
